import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.*;

import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import java.util.*;

/*ʣ�������У�1.�ѳ�����������Ӳ���������������������ڼ�¼��������
 * 			 2. ʵ�ֵ������Ѻ��ʼ�����
 * 			 3. ʵ�����������뵼��
 * 			 4.�޸Ĵ��ڹرշ����Ķ���
 */

public class MainMenu {
	private JFrame f;
	private JList list;
	private JPanel jp;
	private JButton newNote, importNote, exportNote;
	
	public MainMenu(){
		f = new JFrame();
		
		ListModel mode = new DataModel();
		list = new JList(mode);
		list .setVisibleRowCount(5);
		list.setBorder(BorderFactory.createTitledBorder("�����������ѣ�"));
		list.addMouseListener(new Adapter());
		
		
		jp = new JPanel();
		newNote = new JButton("�½�����");
		newNote.addActionListener(new Lis());
		newNote.setSize(new Dimension(100, 30));
		importNote = new JButton("��������");
		importNote.addActionListener(new Lis());
		importNote.setSize(new Dimension(100, 30));
		exportNote = new JButton("��������");
		exportNote.addActionListener(new Lis());
		exportNote.setSize(new Dimension(100, 30));
		
	}
	
	public void launch() {
		f.setSize(400, 250);
		f.setLayout(new GridLayout(2, 1));
		f.add(new JScrollPane(list));
		
		jp.add(newNote);
		jp.add(importNote);
		jp.add(exportNote);
		f.add(jp);
		
		f.pack();
		f.setVisible(true);
		f.setLocationRelativeTo(null);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
	}
	
	class Lis implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			if(source == newNote) {
				f.dispose();
				CreateNote myNote = new CreateNote();
				myNote.launch();
			}else if(source == exportNote) {
				try {
					ExportNote myExport = new ExportNote();
					myExport.main(null);
					JOptionPane.showMessageDialog(null, "�����ɹ�", "��ʾ!", JOptionPane.INFORMATION_MESSAGE);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}else if(source == importNote) {
				f.dispose();
				ImportLead myImport;
				try {
					myImport = new ImportLead();
				} catch (RowsExceededException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (WriteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
					ImportLead.main(null);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			
			}
			
		}
		
	}
	
	class Adapter extends MouseAdapter{
		public void mouseClicked(MouseEvent e) {
			if(e.getClickCount() == 2) {
				System.out.println("˫��");
				//������Ӧ��ϸҳ��
				//����
				String title = (String)list.getSelectedValue();
				f.dispose();
				ModifyNote myNote = new ModifyNote(title);
				myNote.launch();
			}
		}
	}

	public static void main(String[] args){
		MainMenu myMenu = new MainMenu();
		myMenu.launch();

	}
}

class DataModel extends DefaultListModel{
	Vector v = new Vector();
	DataModel() {
		
		try {
			DatabaseCtrl.initCon2();;
			v = DatabaseCtrl.getRemindTitle();
			DatabaseCtrl.close2();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		for(int i = 0; i < v.size(); i++)
			addElement((i+1)+ "."+v.get(i));
	}
}






